#include "cudaFFTUtils.h"
#include <stdexcept>

const int MSG_LENGTH = 200;

void ThrowIfPlanIsNotOfType(const CUDAFTPLANHANDLEInternal& plan, CUDAFTPLANHANDLETYPE type)
{
	if (plan.planType != type)
	{
		throw std::logic_error("CU-FFT-plan has wrong type.");
	}
}

void ThrowIfCudaError(cudaError_t cr, const char* functionName)
{
	if (cr != cudaSuccess)
	{
		char msg[MSG_LENGTH];
		_snprintf_s(msg, sizeof(msg), "CUDA-call failed (\"%s\")", (functionName != nullptr) ? functionName : "?");
		throw std::runtime_error(msg);
	}
}

void ThrowIfCuFFTError(cufftResult cr, const char* functionName)
{
	if (cr != CUFFT_SUCCESS)
	{
		char msg[MSG_LENGTH];
		_snprintf_s(msg, sizeof(msg), "CuFFT-call failed (\"%s\")", (functionName != nullptr) ? functionName : "?");
		throw std::runtime_error(msg);
	}
}

void ThrowIfNppError(NppStatus status, const char* functionName)
{
	if (status != NPP_SUCCESS)
	{
		char msg[MSG_LENGTH];
		_snprintf_s(msg, sizeof(msg), "NPP-call failed (\"%s\")", (functionName != nullptr) ? functionName : "?");
		throw std::runtime_error(msg);
	}
}

void ThrowIfNull(void* vp, const char* functionName)
{
	if (vp == NULL)
	{
		char msg[MSG_LENGTH];
		_snprintf_s(msg, sizeof(msg), "Allocation failed (\"%s\")", (functionName != nullptr) ? functionName : "?");
		throw std::runtime_error(msg);
	}
}